package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.SerialPort;
//import edu.wpi.first.wpilibj.*;

public class Serial {

    public SerialPort serial;

    public Serial() {
        System.out.println("NOTHIN!!!");
    }

    public void startPort() {
        try {
            this.serial = new SerialPort(9600);
            this.serial.disableTermination();
            this.serial.setFlowControl(SerialPort.FlowControl.kNone);
            this.serial.setTimeout(10);
            System.out.println("Woot");

        } catch (Exception e) {
            System.out.println("Fail" + e.getMessage());
        }
    }

    public void endCom() {

        try {
            this.serial.free();
        } catch (Exception e) {
            System.out.println("Fail " + e.getMessage());
        }
    }

    public String getData() {
        try {
            return this.serial.readString();
        } catch (Exception e) {
            //System.out.println("Fail " + e.getMessage());
            return null;
        }
    }

    public void resetCom() {
        try {
            this.serial.reset();
            System.out.println("RESET SERIAL");
        } catch (Exception e) {
            System.out.println("Fail " + e.getMessage());
        }
    }

    public boolean sendData(byte[] buffer) throws Exception {
        try {
            int count = buffer.length;
            this.serial.write(buffer, count);
            return true;
        } catch (Exception e) {
            //  System.out.println("Fail " + e.getMessage());
            return false;
        }
    }

    public boolean printf(String data) {
        try {
            this.serial.print(data);
            return true;
        } catch (Exception e) {
            //System.out.println("Fail " + e.getMessage());
            return false;
        }
    }

    public byte[] get1() {
        try {
            return this.serial.read(this.serial.getBytesReceived());
        } catch (Exception e) {
            //System.out.println("Fail " + e.getMessage());
            byte[] t = {1};
            return t;
        }

    }

    public int dataAv() {
        try {
            return this.serial.getBytesReceived();
        } catch (Exception e) {
            //System.out.println("Fail " + e.getMessage());
            return 0;
        }
    }

}